package com.hxb.burp.utils;

import mjson.Json;

/**
 * burp_extendsion2
 *
 * @author 黄小斌
 * @date 2023-10-27
 */
public class AjaxResult {
    private int code;
    private String msg;
    private Object data;


    private boolean success = false;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
        if (0 == code) {
            this.success = true;
        }
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public AjaxResult() {

    }

    public AjaxResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        if (0 == code) {
            this.success = true;
        } else {
            this.success = false;
        }
    }

    public Json getJson() {
        Json json = Json.object();
        json.set("code", code);
        json.set("msg", msg);
        json.set("data", data);
        return json;
    }

    public String toString() {
        return getJson().toString();
    }

    public static AjaxResult success(Object data) {
        return result(0, "success", data);
    }

    public static AjaxResult error(int code, String msg) {
        return result(code, msg, null);
    }

    public static AjaxResult result(int code, String msg, Object data) {
        return new AjaxResult(code, msg, data);
    }
}
